package com.netease.ocr;

import java.io.File;
import java.net.URL;

import org.apache.commons.io.FileUtils;

public class VerifyCodeUtil {
    
	private static OCR ocr;
	public static final String VERIFY_CODE_URL = "http://dynamic.12306.cn/otsweb/passCodeAction.do?rand=sjrand";
	public static final String LOCAL_TMP_FILE = "verifyCode.tiff";
	
	static {
		ocr = new OCR();
	}
	
    public static String crackVerifyCode(String fileName) throws Exception {
		
		File file = new File(fileName);

		/*if (file.exists()) {
			file.delete();
		}
		file.createNewFile();
		// 下载图片
		//FileUtils.copyURLToFile(new URL(VERIFY_CODE_URL), file);
		 * 
		 */
		// 对图片进行预处理
		ImageProcessor.processImg(file);
		
		// 识别验证码
		return ocr.recognizeText(file, "tiff");
	}
    
    public static void main(String[] args) throws Exception {
		String verifyCode = crackVerifyCode(LOCAL_TMP_FILE);
		System.out.println(verifyCode);
	}

}
